<?php
namespace app\oa\controller;



use app\admin\builder\ConfigBuilder;
use app\admin\builder\ListBuilder;
use app\oa\ext\Role;
use app\oa\model\Department;
use app\oa\model\User;
use think\Config;
use think\Request;
use think\Url;

class Admin extends \app\admin\controller\Admin {
    public function departmentList() {
        $data = Department::all();

        $builder = new ListBuilder();
        return $builder
            ->title("部门列表")
            ->buttonNew(Url::build("department"))
            ->buttonDelete(Url::build("departmentDel"))
            ->keyText("name", "部门名称")
            ->keyDoAction(urldecode(Url::build("department", ['id' => "###"])), "编辑", false)
            ->data($data)
            ->fetch();
    }
    public function departmentDel($ids) {
        if(Department::destroy($ids)) {
            $this->success("删除成功");
        }else {
            $this->error("删除失败");
        }
    }
    public function department(Request $request, $id = null){
        if($request->isPost()) {
            if($id === null) {
                $department = new Department();
            }else {
                $department = Department::get($id);
            }
            $department['name'] = $request->param("name");
            if($department->save()) {
                $this->success("操作成功", Url::build("departmentList"));
            } else {
              $this->error("操作失败");
            }

        }else {
            $data = [];
            if($id !== null) {
                $data = Department::get($id);
            }
            $builder = new ConfigBuilder();
            return $builder
                ->title("部门")
                ->keyText("name", "部门名")
                ->data($data)
                ->buttonSubmit()
                ->buttonBack()
                ->fetch();
        }

    }

    public function userList() {
        $data = User::all();

        foreach ($data as &$d) {
            $d['role'] = array_search($d['role'], Role::Select);
            $d['department'] = $d['department']['name'];
        }
        $builder = new ListBuilder();
        return $builder
            ->title("用户列表")
            ->buttonNew('user')
            ->buttonDelete("userDel")
            ->keyText("username", "用户名")
            ->keyText("role", "角色")
            ->keyText("department", "部门")
            ->keyDoAction(urldecode(Url::build("password", ['id'=>'###'])), "重置密码", false)
            ->data($data)
            ->fetch();
    }
    public function userDel($ids) {
        if(User::destroy($ids)) {
            $this->success("删除成功");
        }else {
            $this->error("删除失败");
        }
    }
    public function user(Request $request, $id = null) {
        if($request->isPost()) {
            if($id === null) {
                // 创建
                $user = new User();
            }else {
                // 修改
                $user = User::get($id);
            }
            $user['username'] = $request->param("username");
            $user['role'] = $request->param("role");
            $user['department_id'] = $request->param("department_id");

            if($user->save()) {
                $this->success("操作成功", "userList");
            }else {
                $this->error("操作失败");
            }
        }else {
            $data = [];

            if($id !== null) {
                $data = User::get($id);
            }

            // 部门
            $department = [];
            foreach (Department::all() as $d) {
                $department[$d['name']] = $d['id'];
            }

            $builder = new ConfigBuilder();
            return $builder
                ->title("用户")
                ->keyText("username", "用户名")
                ->keySelect("role", "角色", Role::Select)
                ->keySelect("department_id", "部门", $department)
                ->data($data)
                ->buttonSubmit()
                ->buttonBack()
                ->fetch();
        }
    }
    public function password(Request $request, $id) {
        if($request->isPost()) {
            $user = User::get($id);
            $user['password'] = md5(Config::get("salt") . $request->param("password"));
            if($user->save()) {
                $this->success("重置成功", "userList");
            }else {
                $this->error("重置失败");
            }
        }else {
            $builder = new ConfigBuilder();
            return $builder
                ->title("重置密码")
                ->keyText("password", "密码")
                ->buttonSubmit()
                ->buttonBack()
                ->fetch();
        }
    }
}
